﻿using ACGN.Application.Newsflashs;
using ACGN.Application.Newsflashs.Dtos;
using ACGN.Core.Models;
using ACGN.Core.Repository;
using ACGN.MVC.Models.Newsflashss;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using ACGN.Mvc.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ACGN.MVC.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class NewsFlashController : BaseController
    {
        private readonly ILogger<NewsFlashController> _logger;
        private readonly IRepository<Newsflash, int> _repository;
        private readonly INewsflashService _newsflashService;
        private readonly IMapper _mapper;
        private readonly IRepository<NfType, int> _nfTypeRepository;

        public NewsFlashController(ILogger<NewsFlashController> logger,
            IRepository<Newsflash, int> repository,
            INewsflashService newsflashService,
            IMapper mapper,
            IRepository<NfType,int> nfTypeRepository
            )
        {
            _logger = logger;
            _repository = repository;
            _newsflashService = newsflashService;
            _mapper = mapper;
            _nfTypeRepository = nfTypeRepository;
        }

        public async Task<IActionResult> Index()
        {

            ViewBag.NfTypeList = new MultiSelectList(await _nfTypeRepository.GetAllListAsync(), "NFTId", "NFTName");
            return View();
        }
        public async Task<IActionResult> List(NewsflashInputDto input)
        {
            var newsflash = await _newsflashService.GetPageResult(input);
            return Json(ListResult(_mapper.Map<List<NewsflashViewModel>>(newsflash.Data), newsflash.TotalCount));
        }
        [HttpGet]
        public async Task<IActionResult> Add()
        { 
            ViewBag.NfTypeList = new MultiSelectList(await _nfTypeRepository.GetAllListAsync(), "NFTId", "NFTName");
            return View();
        }
        [HttpPost]
        public async Task<ActionResult<Newsflash>> AddNews(Newsflash newsflash)
        {
            await _repository.InsertAsync(newsflash);
            return CreatedAtAction(nameof(List), new { id = newsflash.NFId }, newsflash);
        }
        [HttpGet]
        public async Task<IActionResult> Edit(int nfId)
        {
            Newsflash newsflash = await _repository.FirstOrDefaultAsync(s => s.NFId == nfId);
            ViewBag.NfTypeList = new MultiSelectList(await _nfTypeRepository.GetAllListAsync(), "NFTId", "NFTName");
            return View(_mapper.Map<NewsflashViewModel>(newsflash));
        }
        [HttpPost]
        public async Task<ActionResult> EditNews(int NFId,Newsflash newsflash)
        {
            if (NFId != newsflash.NFId)
            {
                return Json(AjaxResult(false));
            }
            await _repository.UpdateAsync(newsflash);
            return Json(AjaxResult(true));
        }
        public async Task<ActionResult> Delete(int NFId, Newsflash newsflash)
        {
            if (NFId != newsflash.NFId)
            {
                return Json(AjaxResult(false));
            }
            await _repository.DeleteAsync(newsflash);
            return Json(AjaxResult(true));
        }
    }
}
